<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class CommentNotification extends Notification
{
    use Queueable;
    protected $comment;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($comment)
    {
        //类初始化的时候接收一个参数,并将这个参数赋值给当前对象$this中的comment属性
        $this->comment = $comment;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)->markdown('Models/Comment');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        //打印$notifiable可以发现$notifiable是调用这个通知类的对象,在这里指的是评论所在文章的作者
//        dd($notifiable);
        //user_id指发评论的人
        return [
            //$this->comment是传递过来的comment评论对象,接着通过评论模型对象中user模型关联动态属性可以获取发表评论的用户对象
            'user_id'=>$this->comment->user->id,
            'user_name'=>$this->comment->user->name,
            'user_icon'=>$this->comment->user->icon,
            //将$this->comment评论对象的id也返回
            'comment_id'=>$this->comment->id,
        ];
    }
}





